/******************************************************************************
 ** auth: liukai
 ** date: 2017/7
 ** ver : 1.0
 ** desc:  说明
 ** Copyright © 2017年 尧尚信息科技(wwww.yourshares.cn). All rights reserved
 ******************************************************************************/

import MBProgressHUD
import YSKit

enum HudIcon {
    case loading
    case success
    case failure
    case caution

    var icon: UIImage? {
        switch self {
        case .success:
            return UIImage(named: "hud_success")
        case .failure:
            return UIImage(named: "hud_failure")
        case .caution:
            return UIImage(named: "hud_caution")
        default:
            return UIImage()
        }
    }

    case none
}

let HUD = HudManager()

class HudManager: NSObject {

    fileprivate let customView = UIImageView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30)))

    fileprivate override init() {

        UIActivityIndicatorView.appearance(whenContainedInInstancesOf: [MBProgressHUD.self]).color = UIColor.white
//        UIActivityIndicatorView.us_appearanceWhenContained(in: MBProgressHUD.self).color = UIColor.white
        super.init()
    }

    func show(_ title: String? = nil, detail: String? = nil, delay: TimeInterval = 0, view: UIView = APP.window) {

        self.show(.loading, title: title, detail: detail, delay: delay, view: view)
    }

    func show(_ icon: HudIcon, title: String? = nil, detail: String? = nil, delay: TimeInterval = 1.0, view: UIView = APP.window) {

        let hud = self.hud(view)

        switch icon {
        case .loading:
            hud.mode = .indeterminate
        default:
            hud.mode = .customView

            hud.customView = self.customView
            self.customView.image = icon.icon
            hud.label.textColor = YSColor.c301.color
            hud.detailsLabel.textColor = YSColor.c301.color
        }

        hud.label.text = title
        hud.detailsLabel.text = detail

        hud.show(animated: true)

        if delay > 0.0 {
            hud.hide(animated: true, afterDelay: delay)
        }
    }

    func toast(_ text: String, delay: TimeInterval = 1.2) {

        let hud = self.hud()

        hud.mode = .text
        hud.label.text = text
        hud.label.textColor = YSColor.c301.color
        hud.offset.y = -(UIScreen.main.bounds.height * 0.3)

        hud.show(animated: true)

        if delay > 0.0 {
            hud.hide(animated: true, afterDelay: delay)
        }
    }

    func themeToast(_ text: String, delay: TimeInterval = 1.2) {

        let hud = self.hud()

        hud.mode = .text
        hud.label.text = text
        hud.label.textColor = YSColor.c301.color
        hud.bezelView.backgroundColor = UIColor.lightGray
        hud.offset.y = -(UIScreen.main.bounds.height * 0.3)

        hud.show(animated: true)

        if delay > 0.0 {
            hud.hide(animated: true, afterDelay: delay)
        }
    }

    func hide(_ delay: TimeInterval = 0.0, view: UIView = APP.window) {

        guard let hud = MBProgressHUD(for: view) else {
            return
        }

        hud.hide(animated: true, afterDelay: delay)
    }

    func hud(_ view: UIView = APP.window) -> MBProgressHUD {

        var hud: MBProgressHUD!
        if let h = MBProgressHUD(for: view) {
            hud = h
        } else {
            hud = MBProgressHUD.showAdded(to: view, animated: true)
            hud.bezelView.style = .solidColor
            hud.bezelView.backgroundColor = UIColor(white: 0, alpha: 0.5)
            hud.minShowTime = 0.5
            hud.animationType = .fade
            hud.label.font = YSFont.t05.font
            hud.detailsLabel.font = YSFont.t06.font
            hud.label.textColor = YSColor.c301.color
            hud.detailsLabel.textColor = YSColor.c301.color
        }

        return hud
    }
}
